package com.wei.c.core;

import java.util.WeakHashMap;

import android.app.Application;
import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.Volley;
import com.evergreen.locker.data.config.LockerServerConfig;

public class App extends Application {
    private static App sInstance;

    public static App get() {
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        initConfig();
    }

    private void initConfig() {
        //...
    }

    private static final int sCacheSize = 12 * 1024 * 1024; // 12MB
    private static final LruCache<String, Bitmap> sImgLruCache = new LruCache<String, Bitmap>(sCacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getByteCount();
        };
    };

    private static final ImageCache sImageCache = new ImageCache() {
        @Override
        public void putBitmap(String key, Bitmap value) {
            sImgLruCache.put(key, value);
        }
        @Override
        public Bitmap getBitmap(String key) {
            return sImgLruCache.get(key);
        }
    };

    private static WeakHashMap<Object, ImageLoader> sImageLoaderRef = new WeakHashMap<Object, ImageLoader>();
    private static WeakHashMap<Object, RequestQueue> sRequestQueueRef = new WeakHashMap<Object, RequestQueue>();
    public static ImageLoader getImageLoader(Object key, RequestQueue requestQueue) {
        if (key == null) throw new IllegalArgumentException("key不能为null");
        ImageLoader iloader = sImageLoaderRef.get(key);
        if (iloader == null) {
            synchronized (sImageLoaderRef) {
                if (iloader == null) {
                    iloader = new ImageLoader(requestQueue == null ? getRequestQueue(key) : requestQueue, sImageCache);
                    sImageLoaderRef.put(key, iloader);
                }
            }
        }
        return iloader;
    }

    /**取得与指定对象关联的ImageLoader**/
    public static ImageLoader getImageLoader(Object key) {
        return getImageLoader(key, null);
    }

    public static RequestQueue getRequestQueue(Object key) {
        RequestQueue queue = sRequestQueueRef.get(key);
        if (queue == null) {
            synchronized (sRequestQueueRef) {
                if (queue == null) {
                    queue = Volley.newRequestQueue(get());
                    sRequestQueueRef.put(key, queue);
                }
            }
        }
        return queue;
    }

    /**取得全局公用的ImageLoader**/
    public static ImageLoader getImageLoader() {
        return getImageLoader(get());
    }

    public static RequestQueue getRequestQueue() {
        return getRequestQueue(get());
    }
}
